//转换后端传来的一菜单对象的工具类
import {getRequest} from "@/util/api";


export const initMenu=(router,store) => {
    //如果但的store有值的话 就不执行不去发送请求了
    if(store.state.routes.length > 0){
        return;
    }
    //发送请求
    getRequest("/system/config/menu").then(data => {
        //如果请求中有数据，就做数据处理
        if(data){
            let fmtRoutes = formatRoutes(data);//进行数据格式化
            router.addRoutes(fmtRoutes);//把数组添加进去给全局Router使用(route)
            store.commit("initRoutes",fmtRoutes)
            store.dispatch('connect');//吃法链接
        }
    })
}

//格式化处理的方法
export const formatRoutes = (menu) => {
    let fmRoutes = [];//需要返回的数据
    menu.forEach(m => {
        //批量的变量定义(相当于 let path = route.path.....)
        let{
            name,
            path,
            component,
            meta,
            iconCls,
            children,
        } = m;
        //判断children是否有对应值
        //递归调用，如果满足条件，那么就重复格式化对应的children集合
        if(children && children instanceof Array){
            children = formatRoutes(children);//
        }
        //处理后的对象
        let fmRoute = {
            path: path,
            name: name,
            iconCls: iconCls,
            meta: meta,
            children: children,
            /*component(){//这里使用了的路组件的"懒加载"
                if(component.startsWith("Home")){
                    import("../views/"+component+".vue")
                }else if (component.startsWith("Emp")) {
                    import ("../views/emp/"+component+".vue")
                } else if (component.startsWith("Per")) {
                    import("../views/per/"+component+".vue")
                } else if (component.startsWith("Sal")) {
                    import("../views/sal/"+component+".vue")
                } else if (component.startsWith("Sta")) {
                    import("../views/sta/"+component+".vue")
                } else if (component.startsWith("Sys")) {
                    import("../views/sys/"+component+".vue")
                }
            },*/
            /** 这里不太懂 为什么不能使用Import来导入？？*/
            component(resolve) {
                //这里注意先路由Home才能继续路由，不然Vue不知道路由那个组件
                if (component.startsWith("Home")) {
                    require(['../views/' + component + '.vue'], resolve);
                } else if (component.startsWith("Emp")) {
                    require(['../views/emp/' + component + '.vue'], resolve);
                } else if (component.startsWith("Per")) {
                    require(['../views/per/' + component + '.vue'], resolve);
                } else if (component.startsWith("Sal")) {
                    require(['../views/sal/' + component + '.vue'], resolve);
                } else if (component.startsWith("Sta")) {
                    require(['../views/sta/' + component + '.vue'], resolve);
                } else if (component.startsWith("Sys")) {
                    require(['../views/sys/' + component + '.vue'], resolve);
                }
            }//组件懒加载导入方式
        }
        fmRoutes.push(fmRoute);//把封装好Routes的数据放到数组中去
    })
    return fmRoutes
}
